/**
*Native和WebView的连接对象
*/
var zs_bridge = new ZsBridge();
/**
*Native和WebView的连接对象的构造函数
*不需要调用，直接使用 cmsbridge对象。
*/
function ZsBridge(){
	var bridge = {};
	bridge.funcArray = new Array();
	bridge.backArray = new Array();
	bridge.index = 0;
	bridge.callFunc = _callFunc;
	bridge.addFunc = _addFunc;
	bridge.onCallback = _onCallback;
	bridge.jsCall = _jsCall;

	bridge.onReturn = _onReturn;
	bridge.onError = _onError;
	bridge.onFail = _onFail;
	bridge.onSuc = _onSuc;
	bridge.onComplete = _onComplete;
	bridge.onCancle = _onCancle;
	bridge.onTrigger = _onTrigger;

	bridge.init = _init;
	bridge.init();

	return bridge;
}
/**
*提供给Native代码调用的接口
*param：Json 格式String类型数据
*/
function ZsOnReturn(id,name,param){
	zs_bridge.onReturn(id,name,param);
}
/**
*提供给Native代码调用的接口
*param：Json 格式String类型数据
*/
function ZsJsCall(func,id,param){
	zs_bridge.jsCall(func,id,param);
}

function _jsCall(func,id,param){
	var item = this.funcArray[func];
    if(item != undefined && typeof(item) == 'function'){
    	var re = item.call(this,param);
    	this.onCallback(id,'suc',re);
    }
}
/**
* 本地代码调用js函数之后的返回
*/
function _onCallback(id,state,param){

}

/**
* 调用本地代码后的返回值
*/
function _onReturn(id,name,sparam){
		var param ;
    	try{ param=eval('('+sparam+')');}
    	catch(exception){
    	param=sparam;
    	}

	var ask = this.backArray[id];
	if (ask != undefined) {
		if(this.isDebug == true){
			alert("func="+ask.name+"\nreturn="+name+"\nparam="+sparam);
		}
		if(name == 'suc'){
			this.onSuc(ask,param);
		}
		else if(name == 'fail'){
			this.onFail(ask,param.errMsg);
		}
		else if(name == 'complete'){
			this.onComplete(ask,param);			
		}
		else if(name == 'cancle'||name == 'cancel'){
			this.onCancle(ask,param);
		}
		else if(name == 'trigger'){
			this.onTrigger(ask,param);
		}
	}
}
/**
* 添加给本地代码调用的功能函数
*/
function _addFunc(name,func){
	this.funcArray[name]=func;
}

/**
* Js调用本地代码
* #name: 函数名称
* #param:函数参数
* #suc:函数调用成功时的回调函数
* #fail:函数调用失败时的回调函数
* #complete:函数调用完成时的回调函数
* #cancle:客户取消时的回调函数
* #trigger：客户操作后的回调函数
*/
function _callFunc(name,param,suc,fail,complete,cancle,trigger){
	/*Native调用Js提供的功能*/
	var id = ++this.index;
	var ask = {
		id:id,
		name:name,
		suc:suc,
		fail:fail,
		complete:complete,
		cancle:cancle,
		trigger:trigger
	}
	this.backArray[id] = ask;

	if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
		/*alert("IOS");*/
		//采用此方法时，不支持同时调用多次
        //window.location.href="http:zshost/cms?func="+name+'&id='+id+'&param='+encodeURIComponent(JSON.stringify(param));
        
        //应该采用此模式，可支持同时调用多次
        var msgDiv = document.createElement('div');
        msgDiv.style.display = 'none';
        var messagingIframe = document.createElement('iframe');
        messagingIframe.src = "http://zshost/cms?func="+name+'&id='+id+'&param='+encodeURIComponent(JSON.stringify(param));
        msgDiv.appendChild(messagingIframe);
        document.documentElement.appendChild(msgDiv);
        // alert("js call herf");
        
        //第三种方法，此方法暂时不能用
//        var execXhr = new XMLHttpRequest();
//        execXhr.open('HEAD', "cmsbridge://" + (+new Date()), true); //设置scheme
//        execXhr.setRequestHeader('id', id);//设置参数等
//        execXhr.setRequestHeader('param', JSON.stringify(param));
//        // 发起请求
//        execXhr.send(null);
        
	}
	else if (navigator.userAgent.match(/android/i)) {			
		if(window.cms_bridge == undefined){
			//针对android4.2以下系统，不用采用addJavaInterface方式注入java对象，因此需要采用onJsPrompt方式注入传递参数
			prompt("http://zshost/cms?func="+name+'&id='+id+'&param='+encodeURIComponent(JSON.stringify(param)));
			return;
		}
		window.cms_bridge.bridge(name,id,JSON.stringify(param));
	}
	else{
		this.onError("不支持的浏览器类型",ask);
	}
	return;
}
/**
* 错误处理
*/
function _onError(msg,ask){
	this.onFail(ask,msg);
	this.onComplete(ask);
}

function _onFail(ask,msg){
	if(ask == undefined)
		return;
	ask.result = false;
	if(ask.fail != undefined && typeof(ask.fail) == 'function'){
		ask.fail.call(this,{errMsg:msg});
	}
}
function _onSuc(ask,param){
	if(ask == undefined)
		return;
	ask.result = true;
	if(ask.suc != undefined && typeof(ask.suc) == 'function'){
		if(param == undefined){
			param = {};
		}
		param.errMsg = ask.name+':ok';
		ask.suc.call(this,param);
	}
}
function _onTrigger(ask,param){
	if(ask == undefined)
		return;
	if(ask.trigger != undefined && typeof(ask.trigger) == 'function'){
		if(param == undefined){
			param = {};
		}
		param.errMsg = ask.name+':trigger';
		ask.trigger.call(this,param);
	}
}
function _onCancle(ask,param){
	if(ask == undefined)
		return;
	if(ask.cancle != undefined && typeof(ask.cancle) == 'function'){
		if(param == undefined){
			param = {};
		}
		param.errMsg = ask.name+':cancle';
		ask.cancle.call(this,param);		
	}
	this.backArray.remove(ask);
}
function _onComplete(ask){
	if(ask == undefined)
		return;
	if(ask.complete != undefined && typeof(ask.complete) == 'function'){
		ask.complete.call(this,{});
	}
	if(ask.result == false || ask.trigger == undefined || typeof(ask.trigger) != 'function'){
		this.backArray.remove(ask);
	}
}

function _init(){
	if (Array.prototype.indexOf == undefined) {
		Array.prototype.indexOf = function(e){
		  for(var i=0,j; j=this[i]; i++){
		    if(j==e){return i;}
		  }
		  return -1;
		};
	}

	Array.prototype.remove = function(b) { 
		var a = this.indexOf(b);
		if (a >= 0) {
		    this[a]=undefined;
			return true; 
		}
		return false; 
	};
}